package com.longrise.android.byjk.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.NetUtil;

/**
 * Created by callxkj on 16/7/5.
 * 加载中 加载失败 加载成功的页面
 */
public class BaseFrameView extends FrameLayout implements View.OnClickListener {
    private final Context mContext;
    private View mLoadingView;
    private View mLoadingErrorView;
    private ReloadListener mReloadListener;

    private TextView mTvError1;
    private TextView mTvError2;

    public BaseFrameView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public BaseFrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public BaseFrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    /**
     * 正在加载的状态
     */
    public enum LoadingStatus {
        LOADING, ERROR, SUCCESS;
    }

    /**
     * 将view添加到容器中
     */
    private void initView() {
        mLoadingView = View.inflate(mContext, R.layout.app_loading_layout, null);
        mLoadingView.setVisibility(View.INVISIBLE);
        addView(mLoadingView);
        mLoadingErrorView = View.inflate(mContext, R.layout.app_loading_error_layout, null);
        initErrorHint();
        mLoadingErrorView.setVisibility(View.INVISIBLE);
        addView(mLoadingErrorView);
        setLoadingStatus(LoadingStatus.LOADING);
    }

    private void initErrorHint() {
        mTvError1 = (TextView) mLoadingErrorView.findViewById(R.id.network_error_tv1);
        mTvError2 = (TextView) mLoadingErrorView.findViewById(R.id.network_error_tv2);
    }

    public void setErrorHint1(String hint){
        if(mTvError1 != null){
            mTvError1.setText(hint);
        }
    }

    public void setErrorHint2(String hint){
        if(mTvError2 != null){
            mTvError2.setText(hint);
        }
    }

    /**
     * 设置加载的状态
     *
     * @param status
     */
    public void setLoadingStatus(LoadingStatus status) {
        switch (status) {
            case LOADING:
                mLoadingErrorView.setVisibility(View.INVISIBLE);
                mLoadingView.setVisibility(View.VISIBLE);
                break;

            case ERROR:
                mLoadingView.setVisibility(View.INVISIBLE);
                mLoadingErrorView.setVisibility(View.VISIBLE);
                mLoadingErrorView.setOnClickListener(this);
                break;

            case SUCCESS:
                mLoadingView.setVisibility(View.INVISIBLE);
                mLoadingErrorView.setVisibility(View.INVISIBLE);
                break;
        }
    }

    /**
     * sjz
     */
    public void setLoadingViewVisiable(boolean visiable) {
        mLoadingView.setVisibility(visiable ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (mReloadListener == null) {
            return;
        }
        if (checkNetWorkEnable()) {
            setLoadingStatus(LoadingStatus.LOADING);
            //重写onReload()，不需要再次做网络检查
            mReloadListener.onReload(v);
        }
    }

    /**
     * 检查network
     */
    private boolean checkNetWorkEnable() {
        if (NetUtil.checkNetEnable()) {
            return true;
        }
        return false;
    }

    public void setOnReloadListener(ReloadListener listener) {
        this.mReloadListener = listener;
    }

    public interface ReloadListener {

        /**
         * 在该方法中不用再次做网络检查
         */
        void onReload(View v);
    }
}
